/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package SnpDbHandling;

import java.util.logging.*;
import java.util.Properties;
import java.io.FileInputStream;
import java.io.IOException;

/**
 *
 * @author Łukasz 'byku' Guzik
 */
public class DBHndLogger
{
    private static final Logger LOGGER = Logger.getLogger("dbHndLogger");
    private static boolean isAfterSetup = false;
    private static Properties logProps;

    private static void LoadProperties() throws IOException
    {
	FileInputStream propsIn = new FileInputStream("logging.properties");
	logProps = new Properties();
	logProps.load(propsIn);
	propsIn.close();
    }

    private static void Setup() throws IOException
    {
	LoadProperties();

	int limit = Integer.parseInt(logProps.getProperty("Limit", "10000"));
	int count = Integer.parseInt(logProps.getProperty("Count", "1"));
	boolean append = Boolean.parseBoolean(logProps.getProperty("Append", "false"));

	Handler fh = new FileHandler(logProps.getProperty("FileName", "defaultLogFile"), limit, count, append);
	fh.setFormatter(new SimpleFormatter());
	fh.setLevel(Level.parse(logProps.getProperty("Level", "OFF")));
	Logger.getLogger("").addHandler(fh);

	LOGGER.setLevel(Level.parse(logProps.getProperty("Level", "OFF")));

	isAfterSetup = true;
    }

    public static Logger getLOGGER() throws IOException
    {
	if(!isAfterSetup)
	    Setup();

	return LOGGER;
    }

    public static void logMessage(Level logLevel, String message)
    {
	LOGGER.log(logLevel, message);
    }

    public static void logException(Level logLevel, Exception e)
    {
	String eol = System.getProperty("line.separator");
	String logMsg = e.toString() + eol + "at" + eol;

	//e.fillInStackTrace();
	StackTraceElement[] ste = e.getStackTrace();
	for(int i = 0; i < ste.length; i++)
	    logMsg = logMsg + "\t" + ste[i].toString() + eol;

	logMessage(logLevel, logMsg);
    }
}